#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

int main() {
    int fd = open("file.txt", O_RDWR);
    if (fd == -1) {
        perror("open");
        return 1;
    }

    struct stat st;
    if (fstat(fd, &st) == -1) {
        perror("fstat");
        close(fd);
        return 1;
    }
    off_t fileSize = st.st_size - 3;

    char *map = mmap(NULL, fileSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (map == MAP_FAILED) {
        perror("mmap");
        close(fd);
        return 1;
    }

    strncpy(map, "Hello", 5);

    if (msync(map, fileSize, MS_SYNC) == -1) {
        perror("msync");
        munmap(map, fileSize);
        close(fd);
        return 1;
    }

    munmap(map, fileSize);
    close(fd);
    return 0;
}
